//
//  XFNetworkTools.m
//  SinaBlog_OC
//
//  Created by macs on 15/11/17.
//  Copyright © 2015年 BlackTail. All rights reserved.
//

#import "XFNetworkTools.h"

static NSString *blogDomain = @"com.baidu.data.error";

@protocol XFNetworkToolsProxy <NSObject>

@optional
- (NSURLSessionDataTask *)
dataTaskWithHTTPMethod:(NSString *)method
             URLString:(NSString *)URLString
            parameters:(id)parameters
               success:(void (^)(NSURLSessionDataTask *, id))success
               failure:(void (^)(NSURLSessionDataTask *, NSError *))failure;

@end

@interface XFNetworkTools () <XFNetworkToolsProxy>

@end

@implementation XFNetworkTools

static XFNetworkTools *singleTools = nil;
+ (instancetype)shareNetWorkTools {

  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
    //末尾要加"/"
    singleTools = [[self alloc]
        initWithBaseURL:[NSURL URLWithString:@"https://api.weibo.com/"]];
    //设置反序列化的格式
    singleTools.responseSerializer.acceptableContentTypes = [NSSet
        setWithObjects:@"application/json", @"text/json", @"text/javascript",
                       @"text/html", @"text/plain", nil];
  });
  return singleTools;
}

- (void)requestJsonDictWithHttpMothod:(HTTPMethod)httpMethod
                            Urlstring:(NSString *)urlString
                           Parameters:(NSDictionary *)parameters
                          AndFinished:(Finished)finished {
  //  NSString *methodName = (httpMethod == HTTPMethodGET) ? @"GET" : @"POST";
  //
  //  [self dataTaskWithHTTPMethod:methodName
  //      URLString:urlString
  //      parameters:parameters
  //      success:^(NSURLSessionDataTask *task, id responseObject) {
  //        if ([responseObject isKindOfClass:[NSDictionary class]]) {
  //          finished(nil, responseObject);
  //        } else {
  //          NSError *dataError = [[NSError alloc]
  //              initWithDomain:blogDomain
  //                        code:-10000
  //                    userInfo:@{
  //                      NSLocalizedDescriptionKey : @"数据错误"
  //                    }];
  //          finished(dataError, nil);
  //        }
  //
  //      }
  //      failure:^(NSURLSessionDataTask *task, NSError *error) {
  //        finished(error, nil);
  //      }];

  if (httpMethod == HTTPMethodPOST) {
    [self POST:urlString
        parameters:parameters
        success:^(NSURLSessionDataTask *_Nonnull task,
                  id _Nonnull responseObject) {
          if ([responseObject isKindOfClass:[NSDictionary class]]) {
            finished(nil, responseObject);
          } else {
            NSError *dataError = [[NSError alloc]
                initWithDomain:blogDomain
                          code:-10000
                      userInfo:@{
                        NSLocalizedDescriptionKey : @"数据错误"
                      }];
            finished(dataError, nil);
          }

        }
        failure:^(NSURLSessionDataTask *_Nullable task,
                  NSError *_Nonnull error) {
          finished(error, nil);
        }];
  } else {
    [self GET:urlString
        parameters:parameters
        success:^(NSURLSessionDataTask *_Nonnull task,
                  id _Nonnull responseObject) {

          if ([responseObject isKindOfClass:[NSDictionary class]]) {
            //成功的回调
            finished(nil, responseObject);
          } else {
            NSError *dataError = [[NSError alloc]
                initWithDomain:blogDomain
                          code:-10000
                      userInfo:@{
                        NSLocalizedDescriptionKey : @"数据错误"
                      }];
            finished(dataError, nil);
          }
        }
        failure:^(NSURLSessionDataTask *_Nullable task,
                  NSError *_Nonnull error) {
          finished(error, nil);
        }];
  }
}

- (void)uploadImageWithUrlstring:(NSString *)urlString
                      Parameters:(NSDictionary *)parameters
                       ImageData:(NSData *)imageData
                     AndFinished:(Finished)finished {

  [self POST:urlString
      parameters:parameters
      constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
        [formData appendPartWithFileData:imageData
                                    name:@"pic"
                                fileName:@"tupian"
                                mimeType:@"image/jpeg"];
      }
      success:^(NSURLSessionDataTask *_Nonnull task,
                id _Nonnull responseObject) {
        finished(nil, responseObject);
      }
      failure:^(NSURLSessionDataTask *_Nullable task, NSError *_Nonnull error) {
        finished(error, nil);
      }];
}

@end
